package com.hig.exam.service.impl;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.gson.Gson;
import com.hig.exam.domain.*;
import com.hig.questions.domain.ExamQuestionsProperty;
import com.hig.questions.service.IExamQuestionsPropertyService;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hig.exam.service.IExamTaskDataService;
import com.hig.exam.service.IExamTaskGroupService;
import com.hig.exam.service.IExamTaskManagerService;
import com.hig.exam.service.IExamTaskPersonService;
import com.hig.exam.service.IExamTaskQuestionsService;

@Service
public class ExamTaskDataServiceImpl implements IExamTaskDataService {
    @Autowired
    private IExamTaskGroupService examTaskGroupService;

    @Autowired
    private IExamTaskManagerService examTaskManagerService;

    @Autowired
    private IExamTaskQuestionsService examTaskQuestionsService;

    @Autowired
    private IExamTaskPersonService examTaskPersonService;

	@Autowired
	private TokenService tokenService;

	@Autowired
	private IExamQuestionsPropertyService propertyService;


	@Override
	public int saveData(ExamTaskData examTaskData) throws Exception {
		// TODO Auto-generated method stub
		ExamTaskManager examTaskManager = examTaskData.getExamTaskManager();
		Gson gson = new Gson();
		examTaskManager.setRangesJson(gson.toJson(examTaskManager.getRanges()));
		int count = 0;
		try
		{
			// 安全删除
			examTaskManagerService.deleteExamTaskManagerById(examTaskManager.getExamCode());
			
			// 保存数据
			count = examTaskManagerService.insertExamTaskManager(examTaskManager);
		}
		catch(Exception e) {
			throw new Exception("保存属性出错,信息:" + e.getMessage());
		}
		
		List<ExamTaskQuestions> questionsList = examTaskData.getTaskQuestionsList();
		
		// 安全删除
		examTaskQuestionsService.deleteExamTaskQuestionsById(examTaskManager.getExamCode());
		
		for(ExamTaskQuestions examTaskQuestions: questionsList) {
			try
			{
//查询题目类型

				ExamQuestionsProperty examQuestionsProperty = propertyService.selectExamQuestionsPropertyById(examTaskQuestions.getQuestionsCode());
				if (ObjectUtil.isEmpty(examQuestionsProperty)) {
					throw new RuntimeException("保存题目信息出错,查不到题目信息");
				}
				//2  单选 1 判断 3 多选
				//
				Integer questionsType = examQuestionsProperty.getQuestionsType();
				if (questionsType == 1) {
					examTaskQuestions.setScore(ObjectUtil.isNull(examTaskManager.getTfScore())?new BigDecimal("2"):examTaskManager.getTfScore());
				}else if (questionsType == 2) {
					examTaskQuestions.setScore(ObjectUtil.isNull(examTaskManager.getSingleScore())?new BigDecimal("2"):examTaskManager.getSingleScore());
				}else {
					examTaskQuestions.setScore(ObjectUtil.isNull(examTaskManager.getMultiScore())?new BigDecimal("2"):examTaskManager.getMultiScore());
				}
				count = examTaskQuestionsService.insertExamTaskQuestions(examTaskQuestions);
			}
			catch(Exception e) {
				throw new Exception("保存题目信息出错,信息:" + e.getMessage());				
			}
		}
		
		List<ExamTaskGroup> groupList = examTaskData.getTaskGroupList();
if (CollectionUtil.isNotEmpty(groupList)){
	// 安全删除
	examTaskGroupService.deleteExamTaskGroupById(examTaskManager.getExamCode());

	for (ExamTaskGroup examTaskGroup: groupList) {
		try
		{
			count = examTaskGroupService.insertExamTaskGroup(examTaskGroup);
		}
		catch(Exception e) {
			throw new Exception("保存分组信息出错,信息:" + e.getMessage());
		}
	}
}

		
		// 安全删除
		examTaskPersonService.deleteExamTaskPersonById(examTaskManager.getExamCode());
		
		try
		{
			if (CollectionUtil.isNotEmpty(groupList)){
				count = examTaskPersonService.batchInsertPerson(examTaskManager.getExamCode());
			}else {
				ExamTaskPerson examTaskPerson = new ExamTaskPerson();
				examTaskPerson.setExamCode(examTaskManager.getExamCode());
				LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
				examTaskPerson.setuserCode(loginUser.getUser().getUserCode());
				examTaskPersonService.insertPerson(examTaskPerson);
			}

		}
		catch(Exception e) {
			throw new Exception("保存考试人员信息出错,信息:" + e.getMessage());											
		}
		
		return 1;
	}

}
